/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package graphics;

import graphics.support.ChildEvent;
import graphics.support.ChildrenGraphActionListener;
import graphics.support.CreateDataKineticsGraph;
import graphics.support.CreateDataSpectraGraph;
import graphics.support.WorkingAreaConverter;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import javax.swing.SpinnerNumberModel;
import static javax.swing.WindowConstants.HIDE_ON_CLOSE;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author asus
 */
public class Kineticsgraph extends javax.swing.JFrame {

    private ChildrenGraphActionListener listener;
    private ChartPanel KineticsChartPanel;
    private JFreeChart KineticsChart;
    private double[][] data;
    private int[][] Iworkingarea;
    private double[] AwaveLength;
    private double[] AtimeDelays;
    private double max_timedelay;
    private double min_timedelay;
    private XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer();
    private XYPlot xyplot;
    private String typeYaxis;
    private XYSeriesCollection xyseriescollection = new XYSeriesCollection();
    private NumberAxis xAxis = new NumberAxis("Time delay,~s");
    private NumberAxis yAxis = new NumberAxis("Intensity");
    private SpinnerNumberModel spinnerModel;

    /**
     * Creates new form Kineticsgraph
     *
     * @param workingarea
     */
    public void setWorkingArea(double[][] workingarea) {
        this.Iworkingarea = WorkingAreaConverter.convertToindexes(AwaveLength, AtimeDelays, workingarea, typeYaxis);
        xyseriescollection = new CreateDataKineticsGraph().createData(data, Iworkingarea, AwaveLength, AtimeDelays, xyseriescollection, false);
        xyplot.setDataset(xyseriescollection);
    }

    public Kineticsgraph(double[][] data, double[] AwaveLength, double[] AtimeDelays, double[][] workingarea, double max_timedelay, double min_timedelay, String typeYaxis, ChildrenGraphActionListener listener) {
        initComponents();
        myinit(data, AwaveLength, AtimeDelays, workingarea, max_timedelay, min_timedelay, typeYaxis, listener);

    }

    /**
     *
     * @param data
     * @param AwaveLength
     * @param AtimeDelays
     * @param max_timedelay
     * @param workingarea
     * @param min_timedelay
     * @param typeYaxis
     */
    public final void myinit(double[][] data, double[] AwaveLength, double[] AtimeDelays, double[][] workingarea, double max_timedelay, double min_timedelay, String typeYaxis, ChildrenGraphActionListener varlistener) {
        this.listener = varlistener;
        this.typeYaxis = typeYaxis;
        this.max_timedelay = max_timedelay;
        this.min_timedelay = min_timedelay;
        this.AwaveLength = AwaveLength;
        this.AtimeDelays = AtimeDelays;
        this.data = data;
        this.Iworkingarea = WorkingAreaConverter.convertToindexes(AwaveLength, AtimeDelays, workingarea, typeYaxis);
        xyseriescollection = new CreateDataKineticsGraph().createData(data, Iworkingarea, AwaveLength, AtimeDelays, xyseriescollection, false);
        KineticsChartPanel = new ChartPanel(createKineticsGraph());
        mainPanel.add(KineticsChartPanel);
        pack();

        kineticsCounter.setValue(xyseriescollection.getSeries().size());
        spinnerModel = new SpinnerNumberModel(1, 1, 1, 1);

        kineticsCounter.setModel(spinnerModel);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                setDefaultCloseOperation(HIDE_ON_CLOSE);
                listener.onChildevent(new ChildEvent(false));
            }

        });

    }

    private JFreeChart createKineticsGraph() {
        xAxis.setRange(AtimeDelays[(int) min_timedelay], AtimeDelays[(int) max_timedelay]);
        xyplot = new XYPlot(xyseriescollection, xAxis, yAxis, xylineandshaperenderer);
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setDomainGridlinePaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.white);
        KineticsChart = new JFreeChart("KineticsGraph", xyplot);
        KineticsChart.setBackgroundPaint(Color.white);
        return KineticsChart;
    }

    private void setxyseriescollection() {
        int size = xyseriescollection.getSeries().size();
        if (size == 2) {
            size = 3;
        }
        kineticsCounter.setValue(size);
        spinnerModel = new SpinnerNumberModel(size - 2, 1, size - 2, 1);
        kineticsCounter.setModel(spinnerModel);
        xyplot.setDataset(xyseriescollection);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        kineticsCounter = new javax.swing.JSpinner();
        AddButton = new javax.swing.JButton();
        DeleteButton = new javax.swing.JButton();
        AllCheckbox = new javax.swing.JCheckBox();

        mainPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        mainPanel.setLayout(new java.awt.BorderLayout());

        AddButton.setText("Add");
        AddButton.setToolTipText("");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        DeleteButton.setText("Delete");
        DeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteButtonActionPerformed(evt);
            }
        });

        AllCheckbox.setText("All?");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(kineticsCounter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(DeleteButton)
                    .addComponent(AllCheckbox)
                    .addComponent(AddButton))
                .addGap(18, 18, 18)
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 579, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(AddButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(kineticsCounter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(DeleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(AllCheckbox)
                        .addGap(0, 270, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        xyseriescollection = new CreateDataKineticsGraph().createData(data, this.Iworkingarea, AwaveLength, AtimeDelays, xyseriescollection, true);
        setxyseriescollection();
    }//GEN-LAST:event_AddButtonActionPerformed

    private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteButtonActionPerformed
        XYSeries xyservar = null;
        int j = 0;
        XYSeriesCollection var = new XYSeriesCollection();

        if (AllCheckbox.isSelected() == false) {
            if (xyseriescollection.getSeriesCount() != 2) {
                for (int i = 0; i < xyseriescollection.getSeriesCount(); i++) {

                    if (i != (Integer) kineticsCounter.getValue() - 1) {
                        xyservar = xyseriescollection.getSeries(i);

                        if (i < xyseriescollection.getSeriesCount() - 2) {
                            xyservar = new XYSeries(Integer.toString(j + 1) + ":" + xyseriescollection.getSeries(i).getKey().toString().split(":")[1], false, true);
                            for (int k = 0; k < xyseriescollection.getSeries(i).getItemCount(); k++) {
                                xyservar.add(xyseriescollection.getSeries(i).getDataItem(k));
                            }
                        }
                        var.addSeries(xyservar);
                        j++;
                    }
                }
            }
        }

        if (AllCheckbox.isSelected()) {
            System.out.println(Integer.toString(xyseriescollection.getSeriesCount()));
            xyservar = xyseriescollection.getSeries(xyseriescollection.getSeriesCount() - 2);
            var.addSeries(xyservar);
            xyservar = xyseriescollection.getSeries(xyseriescollection.getSeriesCount() - 1);
            var.addSeries(xyservar);
        }
        xyseriescollection = var;
        //  xyseriescollection = new CreateDataKineticsGraph().createData(data, this.Iworkingarea, AwaveLength, AtimeDelays, var, false);
        setxyseriescollection();
    }//GEN-LAST:event_DeleteButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JCheckBox AllCheckbox;
    private javax.swing.JButton DeleteButton;
    private javax.swing.JSpinner kineticsCounter;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
}
